package com.qd.qx.ui.module1;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.qd.common.base.BaseActivity;
import com.qd.common.commonutils.ToastUitl;
import com.qd.common.commonwidget.LoadingDialog;
import com.qd.qx.R;
import com.qd.qx.api.MyXMPPTCPConnection;
import com.qd.qx.utils.AppKeyBoardMgr;
import com.qd.qx.widget.DeleteEditText;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Code is far away from bug with the animal protecting
 * Author：YS on 2018/10/31
 * Function：修改群聊名称
 */
public class EditGroupNameActivity extends BaseActivity {
    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvMore)
    TextView tvMore;
    @BindView(R.id.ivMore)
    ImageView ivMore;
    @BindView(R.id.etName)
    DeleteEditText etName;
    String jid;

    /**
     * 入口
     */
    public static void startAction(Activity activity, String jid) {
        Intent intent = new Intent(activity, EditGroupNameActivity.class);
        intent.putExtra("jid", jid);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.fade_in,
                com.qd.common.R.anim.fade_out);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_editgroupname;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        tvTitle.setText("修改群名称");
        ivMore.setVisibility(View.GONE);
        tvMore.setVisibility(View.VISIBLE);
        tvMore.setText("完成");
        tvMore.setTextColor(Color.parseColor("#2ff13d"));
        jid = getIntent().getStringExtra("jid");
        AppKeyBoardMgr.openKeybord(etName, this);
        etName.setMaxLength(20);
    }


    @OnClick({R.id.ivBack, R.id.tvMore})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                AppKeyBoardMgr.closeKeybord(etName, this);
                finish();
                break;
            case R.id.tvMore:
                if (TextUtils.isEmpty(etName.getText().toString())) {
                    ToastUitl.showShort("请编写群名称");
                    return;
                }
                LoadingDialog.showDialogForLoading(this);
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        editGroupName();
                    }
                }.start();
                break;
        }
    }

    private void editGroupName() {
        boolean flag = MyXMPPTCPConnection.getInstance().editGroupName(jid, etName.getText().toString().trim());
        LoadingDialog.cancelDialogForLoading();
        if (flag) {
            ToastUitl.showShort("操作成功");
            AppKeyBoardMgr.closeKeybord(etName, this);
            finish();
        } else {
            ToastUitl.showShort("操作失败");
        }
    }
}
